// pages/teacher-info/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
const http = require('../../utils/util.js')
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        banners: [
            urls.apiUrl+'up/admin/0_1562653856.jpg',
            urls.apiUrl+'up/admin/0_1562652106.jpg',
            urls.apiUrl+'up/admin/0_1562653909.jpg'
        ],
        container: "",
        showauth: false, //显示授权登陆,
        currentIndex: 0,
        current: 0,
        tabIcon: [{
                id: 0,
                pic: '/img/homes.png',
                piced: '/img/homesd.png',
                title: '首页',
            },
            {
                id: 1,
                pic: '/img/message.png',
                piced: '/img/messaged.png',
                title: '信息',
            },
            {
                id: 2,
                pic: '/img/mine.png',
                piced: '/img/mined.png',
                title: '我的',
            },
        ],


        friendsLists: [{
                "id": 0,
                "name": "消息"
            },
            {
                "id": 1,
                "name": "追踪"
            },
            {
                "id": 2,
                "name": "访客"
            },
        ],
        webUrl: '',
        alldetail: {},
        visitorList: [],
        visitorPage: 1,
        visitorTotal: 0,
        traceInfo:[],
        tracePage: 1, //追踪页数
        traceTotal: 0, //追踪总页数
    },
    aboutPage: function(e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            tabIcon: this.data.tabIcon,
            currentIndex: e.currentTarget.dataset.id,
        })
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 0
        })
    },
    //今日访客
    todayVisi: function() {
        this.setData({
            currentIndex: 1,
            current: 2
        })
    },
    // 今日预约
    appointment: function() {
        wx.navigateTo({
            url: '/pages/appointment/index?type=teacher',
        })
    },
    //在授课程
    saleClass: function() {
        wx.navigateTo({
            url: '/pages/saleClass/index?teacherId=' + this.data.mineInfo.teacherId,
        })
    },
    // 在职机构
    onInstitu: function() {
        wx.navigateTo({
            url: '/pages/myin/myin',
        })
    },
    //  想学人数
    wantLearn: function() {
        wx.navigateTo({
            url: '/pages/wantStudy/index?id=' + this.data.mineInfo.teacherId + '&type=teacher',
        })
    },
    //改变
    swiper_change(e) {
        this.setData({
            current: e.detail.current
        })
        if (e.detail.current === 0) {
            this.setData({
                container: this.data.messageInfo.length * 145
            })
        } else if (e.detail.current === 1) {
            this.setData({
                container: this.data.traceInfo.length * 145
            })
        } else if (e.detail.current === 2) {
            this.setData({
                container: this.data.info.length * 145
            })
        }
    },
    //信息页面
    seCurrent: function(e) {
        this.setData({
            current: e.currentTarget.dataset.id,
        })
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 0
        })
    },
    //聊天
    goChat: function(e) {
        let uid = e.currentTarget.dataset.uid;
        let name = e.currentTarget.dataset.name;
        let avatar = e.currentTarget.dataset.avatar;
        wx.navigateTo({
            url: '/pages/chat/chat?uid=' + uid + '&name=' + name + '&avatar=' + avatar + '&from=admin&teacherId='+this.data.mineInfo.teacherId
        })
    },
    //課程詳情
    classDetail: function(e) {
        // console.log(e)
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/infoclass/infoclass?courseId=' + id,
        })
    },


    //老師管理端我的活動
    activity: function(teacherId) {
        let that = this
        var coursebyTeacher = loginFn()
        coursebyTeacher.teacherId = teacherId
        wx.request({
            url: urls.coursebyTeacher,
            method: 'GET',
            data: coursebyTeacher,
            success: function(res) {
                // console.log(res, '活動')
                that.setData({
                    funList: res.data.data
                })
            }
        })
    },
    //消息
    messageList: function() {
        let that = this
        app.request({
            url: 'message/listUser',
            sCallback: (res) => {
                if(res.code == 1) {
                    that.setData({
                        messageInfo: res.data
                    })
                }
            }
        })
    },
    //追蹤
    traceList: function() {
        let that = this;
        let page = this.data.tracePage;
        let traceInfo  = this.data.traceInfo;
        if (page > 1 && traceInfo.length >= this.data.traceTotal) {
            return false;
        }

        app.request({
            url: 'visitor/trace',
            data: {
                type: 'teacher',
                page: page
            },
            sCallback: (res) => {
                if(res.code == 1) {
                    page ++;
                    traceInfo = traceInfo.concat(res.data.list);
                    that.setData({
                        traceInfo: traceInfo,
                        tracePage: page,
                        traceTotal: res.data.total_size
                    })
                }
            }
        })
    },
    // 訪客
    visitor: function() {
        let that = this;
        let page = this.data.visitorPage;
        let visitorList = this.data.visitorList;

        if (page > 1 && visitorList.length >= that.data.visitorTotal) {
            return false;
        }

        app.request({
            url: 'visitor/index',
            data: {type: 'teacher'},
            sCallback:(res) => {
                if(res.code == 1) {
                    page ++;
                    visitorList = visitorList.concat(res.data.list);
                    that.setData({
                        page: page,
                        visitorList: visitorList,
                        visitorTotal: res.data.total_size
                    })
                }
            }
        })
    },
    govisitor: function(e) {
        wx.navigateTo({
            url: '/pages/visitor/visitor?uid=' + e.currentTarget.dataset.uid+'&teacherId='+this.data.alldetail.teacherId+'&type=teacher',
        })
    },
    // 老师资料
    teacherMine: function(callback) {
        let that = this;
        app.request({
            url: 'teacher/mine',
            sCallback: (res) => {
                if(res.code == 1) {
                    that.setData({
                        mineInfo: res.data
                    })

                    that.activity(res.data.teacherId)
                    that.teacherInfo(res.data.teacherId)
                }
            }
        })
    },
    // 状态码
    teacherAdminStatus: function(callback) {
        let that = this
        let teacherAdminStatus = loginFn()
        wx.request({
            url: urls.teacherAdminStatus,
            data: teacherAdminStatus,
            success: function(res) {
                // console.log(res, '状态码')
                if(res.data.data.status != 5) {
                    callback && callback();
                }
                that.setData({
                    statusNum: res.data.data.status
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log('options:',options);
        this.setData({
            webUrl: urls.zhaopeixun
        });

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.checkLogin();
    },
    checkLogin(option) {
        var login = setInterval(() => {
            // console.log('checklogin')
            if (wx.getStorageSync('login')) {
                clearInterval(login);
                this.init(option);
            }
        }, 100);
    },
    init(callback) {
        let that = this;
        that.setData({
            visitorList: [],
            visitorPage: 1,
            visitorTotal: 0,
            traceInfo:[],
            tracePage: 1, //追踪页数
            traceTotal: 0, //追踪总页数
        })
        this.teacherAdminStatus(() => {

            var loginInfo = loginFn();
            var date = new Date();

            //获取统计信息
            wx.request({
                url: urls.teacherStatus+'?time='+date.getTime(),
                method: 'POST',
                data: loginInfo,
                success: function(res) {
                    // console.log(res, 'curry')
                    that.setData({
                        info: res.data.data
                    })
                }
            })
            this.messageList()
            // this.activity(options)
            this.traceList()
            this.visitor()
            this.teacherMine()
            callback && callback();
        });
    },
    //  老师首页信息
    teacherInfo: function(teacherId) {
        var that = this;
        var teacherInfo = loginFn()
        teacherInfo.teacherId = teacherId;
        teacherInfo.isAdmin = 1;
        wx.request({
            url: urls.teacherUrl,
            method: 'GET',
            data: teacherInfo,
            success: function(res) {
                // console.log(res, 'xuanxuanxuan')
                if(res.data.code == 0) {
                    if (!app.globalData.allselect.courseMain) {
                        var int = setInterval(function() {
                            // console.log('no-data')
                            if (app.globalData.allselect.courseMain) {
                                clearInterval(int);
                                that.detailFn(res.data.data);
                            }
                        }, 100);
                    } else {
                        that.detailFn(res.data.data)
                    }
                }
               

            }
        })
    },
    //处理detail
    detailFn: function(details) {
        // console.log('detailFn')
        // console.log(details)
        var sels = app.globalData.allselect
        // console.log(sels)
        var imgUrls = []
        imgUrls.push(details.uploadImg)

        for (var i = 0; i < details.teacherImages.length; i++) {
            imgUrls.push(details.teacherImages[i].img)
        }
        var alldetailArry = []
        for (var j = 0; j < sels.courseMain.length; j++) {

            if (details.courseMain == sels.courseMain[j].courseMainId) {
                for (var h = 0; h < sels.courseMain[j].sub.length; h++) {
                    if (sels.courseMain[j].sub[h].courseSubId == details.courseSub)
                        details['major'] = sels.courseMain[j].sub[h].courseSub
                }
            };
        }
        for (var i = 0; i < sels.level.length; i++) {
            for (var j = 0; j < details.teacherEdu.length; j++) {
                if (details.teacherEdu[j].levelId == sels.level[i].levelId) {
                    details.teacherEdu[j].level = sels.level[i].level
                };
            }

        }




        this.setData({
            alldetail: details,
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.init(() => {
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (this.data.currentIndex == 1){

            if(this.data.current == 1) {
            //追踪分布
                this.traceList();
            } else if(this.data.current == 2) {
                this.visitor();
            }
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        this.setData({
            sharetip: false
        })
        return {
            title: '快来快来！老师都在用。'
        }
    },
    shareFn: function() { //分享弹框
        this.setData({
            sharetip: true
        })
    },
    shareCircle: function() { //分享到朋友圈
        wx.navigateTo({
            url: '/pages/share/index?type=teacher&from=main',
        })
    },
    addcharge: function() {
        wx.navigateTo({
            url: '/pages/addteacher/index?byuser=1',
        })
    },
    hideMask() {
        this.setData({
            sharetip: false
        })
    }
})